﻿using Quartz.MUI.ClassLibrary.EFContext;
using Quartz.MUI.ClassLibrary.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quartz.MUI.ClassLibrary.Service
{
    public class EFQuartzLogService : IQuartzLogService
    {
        private QuarzEFContext _quarzEFContext;
        public EFQuartzLogService(QuarzEFContext quarzEFContext)
        {
            _quarzEFContext = quarzEFContext;
        }

        public async Task<ResultQuartzData> AddLog(tab_quarz_tasklog tab_Quarz_Tasklog)
        {
            var result = new ResultQuartzData { status = false, message = "" };
            var exist = await _quarzEFContext.tab_quarz_tasklog.Where(a => a.TaskName == tab_Quarz_Tasklog.TaskName
              && a.GroupName == tab_Quarz_Tasklog.GroupName).OrderByDescending(a => a.id).FirstOrDefaultAsync();
            if (exist != null)
            {
                exist.BeginDate = tab_Quarz_Tasklog.BeginDate;
                exist.EndDate = tab_Quarz_Tasklog.EndDate;
                exist.Msg = tab_Quarz_Tasklog.Msg;
                _quarzEFContext.Update(exist);
                var date = await _quarzEFContext.SaveChangesAsync();
                if (date > 0)
                {
                    result.status = true;
                    result.message = "数据库修改成功!";
                }
            }
            else
            {
                _quarzEFContext.Add(tab_Quarz_Tasklog);
                var date = await _quarzEFContext.SaveChangesAsync();
                if (date > 0)
                {
                    result.status = true;
                    result.message = "数据库添加成功!";
                }
            }
            return result;
        }

        public async Task<tab_quarz_tasklog> Getlastlog(string taskName, string groupName)
        {
            var data = await _quarzEFContext.tab_quarz_tasklog.Where(a => a.TaskName == taskName
              && a.GroupName == groupName).OrderByDescending(a=>a.BeginDate).FirstOrDefaultAsync();
            return data;
        }

        public async Task<ResultData<tab_quarz_tasklog>> GetLogs(string taskName, string groupName, int page, int pageSize = 100)
        {
            //int total = _quarzEFContext.tab_quarz_tasklog.Where(a => a.TaskName == taskName
            //  && a.GroupName == groupName).Count();
            //var pagem = page - 1;
            //var data = await _quarzEFContext.tab_quarz_tasklog.Where(a => a.TaskName == taskName
            //&& a.GroupName == groupName).OrderByDescending(a=>a.id).Skip(pagem * pageSize).Take(pageSize).ToListAsync();
            //ResultData<tab_quarz_tasklog> resultData = new ResultData<tab_quarz_tasklog>() { total = total, data = data };
            //return resultData;

            var data = await _quarzEFContext.tab_quarz_tasklog.Where(a => a.TaskName == taskName
              && a.GroupName == groupName).OrderByDescending(a => a.id).FirstOrDefaultAsync();
            ResultData<tab_quarz_tasklog> resultData = new ResultData<tab_quarz_tasklog>() { total = 1, data = data == null ? new List<tab_quarz_tasklog>() : new List<tab_quarz_tasklog> { data } };
            return resultData;
        }
    }
    }
